package com.gaoxin.api.contronller.educontroller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.gaoxin.api.entity.eduentity.Dept;
import com.gaoxin.api.entity.eduentity.StuScoreEntity;
import com.gaoxin.api.entity.statisentity.Course;
import com.gaoxin.api.entity.stuentity.StuInfoEntity;
import com.gaoxin.api.service.eduservice.QueryStuScoreService;
import com.gaoxin.api.service.stuservice.StuInfoService;
import com.google.common.collect.Maps;

import io.swagger.annotations.Api;

/**
 * 查询学生成绩接口-控制层
 *
 * @author fanyue
 * @email 859485490@qq.com
 * @date 2018-07-12 15:21
 */
@RestController
@RequestMapping("/app/teaching")
@Api(tags = "查询学生成绩")
public class QueryStuScoreController {

	Logger log = Logger.getLogger("查询学生成绩~~~~~~~~~~~log~~~~~~~~");
	@Autowired
	private StuInfoService stuInfoService;
	@Autowired
	private QueryStuScoreService queryStuScoreService;

	@RequestMapping(value = "/queryStuScores", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String Test(@RequestBody Map<String,String> paramMap) {
		String stuNumber = paramMap.get("stuNumber");
		String semester = paramMap.get("semester");
		log.info("查询学生成绩+++++++++" + stuNumber);
		String returnStr = "";
		Map<String, Object> retMap = new HashMap<String, Object>();
		try {
			StuInfoEntity stuInfo = stuInfoService.queryStuInfo(stuNumber);
			if (stuInfo == null) {
				retMap.put("code", "100100");
				retMap.put("errmsg", "未查询到该学号对应学生");
				return JSON.toJSONString(retMap);
			}
			Map<String, Object> stuInfoMap = new HashMap<String, Object>();
			stuInfoMap.put("stuNumber", stuInfo.getStuNumber());
			stuInfoMap.put("stuName", stuInfo.getStuName());
			List<StuScoreEntity> queryStuScore = queryStuScoreService.queryStuScore(stuInfo.getStuCode(), semester);
			stuInfoMap.put("arrayList", queryStuScore);
			retMap.put("code", "0");
			retMap.put("results", stuInfoMap);
			returnStr = JSON.toJSONString(retMap);
		} catch (Exception e) {
			log.warning("查询学生成绩异常。。。");
			e.printStackTrace();
			retMap.put("code", "100201");
			retMap.put("errmsg", "查询学生成绩信息错误");
			returnStr = JSON.toJSONString(retMap);
		}
//		log.info("returnStr+++++++++" + returnStr);
		return returnStr;
	}
	/**
	 * 查询所有的二级院系
	 * @param paramMap
	 * @return
	 */
	@RequestMapping(value = "/queryAllDept", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public String queryAllDept() {
		String jsonData = null;
		try {
			List<Dept> list = queryStuScoreService.queryAllDept();
			Map<String, Object> map = Maps.newHashMap();
			if(list.size()!=0&&list!=null){
				map.put("code", "0");
				map.put("results",list);
				jsonData = JSON.toJSONString(map);
			}else{
				map.put("code", "100202");
				map.put("errmsg", "无院系信息！");
				jsonData = JSON.toJSONString(map);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return jsonData;
	}
	/**
	 * 根据院系查询所属专业信息
	 * @return
	 */
	@RequestMapping(value = "/queryMajor", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public String queryMajor(@RequestBody Map<String,String> paramMap) {
		String deptId = paramMap.get("deptId");
		String jsonData = null;
		try {
			Dept dept = queryStuScoreService.queryMajorByDeptId(deptId);
			Map<String, Object> map = Maps.newHashMap();
			if(dept!=null){
				map.put("code", "0");
				map.put("results",dept);
				jsonData = JSON.toJSONString(map);
			}else{
				map.put("code", "100203");
				map.put("errmsg", "无该院系对应的专业信息！");
				jsonData = JSON.toJSONString(map);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return jsonData;
	}
	/**
	 * 
	 * @return
	 */
	@RequestMapping(value = "/queryCourseScores", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public String queryCourseScores(@RequestBody Map<String,String> paramMap) {
		String [] paramArr = paramMap.get("semester").split("_");
		String courseId = paramMap.get("courseId");
		String jsonData = null;
		try {
			Course course = queryStuScoreService.queryCourseScores(courseId, paramArr[0], paramArr[1]);
			Map<String, Object> map = Maps.newHashMap();
			if(course!=null){
				map.put("code", "0");
				map.put("results",course);
				jsonData = JSON.toJSONString(map);
			}else{
				map.put("code", "100204");
				map.put("errmsg", "无该课程的成绩信息！");
				jsonData = JSON.toJSONString(map);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return jsonData;
	}
	/**
	 * 查询院系课程成绩
	 * @return
	 */
	@RequestMapping(value = "/queryScores", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public String queryScoresByDept(@RequestBody Map<String,String> paramMap) {
		String [] paramArr = paramMap.get("semester").split("_");
		String grade = paramMap.get("grade");
		String deptId = paramMap.get("deptId");
		String stuNumber = paramMap.get("stuNumber");
		String courseId = paramMap.get("courseId");
		String jsonData = null;
		try {
			List<StuScoreEntity> stuScore = queryStuScoreService.queryScores(paramArr[0], paramArr[1], deptId, grade,stuNumber,courseId);
			 List<StuScoreEntity> noExma = queryStuScoreService.queryNoExam(paramArr[0], paramArr[1], deptId, grade, stuNumber,courseId);
			 for (StuScoreEntity stuScoreEntity : noExma) {
				stuScore.add(stuScoreEntity);
			}
			Map<String, Object> map = Maps.newHashMap();
			if(stuScore!=null && stuScore.size()!=0){
				map.put("code", "0");
				map.put("results",stuScore);
				jsonData = JSON.toJSONString(map);
			}else{
				map.put("code", "100205");
				map.put("errmsg", "无法查询到成绩信息！");
				jsonData = JSON.toJSONString(map);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return jsonData;
	}
}
